package bp;

import service.DataSyncService;

/**
 * 
 * <b>类描述：机构、站点、班次票价、电子票信息数据同步的类</b><br/>
 * <b>类名称：</b>DataSyncServiceImpl<br/>
 * <b>创建人：</b><a href="mailto:lanchengkun@nova.net.cn">兰成坤</a><br/>
 * <b>关键修改：</b><br/>
 * <b>修改时间：</b><br/>
 * <b>修改人：</b><br/>
 * 
 */
public class DataSyncBP implements DataSyncService {

	@Override
	public int issyn(String orgcode, String interfacename) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String orgsyn(String xmlString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String stationsyn(String xmlString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String pricesyn(String xmlString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String ticketsyn(String xmlString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String pricedelsyn(String xmlString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String vehiclesyn(String xmlString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String departpassengersyn(String xmlString) {
		// TODO Auto-generated method stub
		return null;
	}

	/*@Override
	public int issyn(String orgcode,String interfacename) {
		log.info("orgcode="+orgcode+",interfacename="+interfacename);
		return 0;
	}

	
	 * orgsyn方法：实现机构数据下载<br/> <br/>
	 * 
	 * @param xmlString
	 * 
	 * @return
	 * 
	 * @see cn.nova.sync.service.DataSyncService#orgsyn(java.lang.String)
	 * 
	 * @exception
	 
	@SuppressWarnings("unchecked")
	public String orgsyn(String xmlString) {
		OrgSynRequest orgreq;
		OrgsynResponse orgres = new OrgsynResponse();
		orgres.setPublicresponse(PublicInfoUtil.newPublicResponse());
		try {
			orgreq = (OrgSynRequest) XmlUtil.toBean(xmlString,
					OrgSynRequest.class);

		} catch (Exception e) {
			orgres.getPublicresponse().setResultcode("1001");
			orgres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("1001") + e.getMessage());
			log.error(orgres.getPublicresponse().getResultmsg() + "请求字符串为:" + xmlString, e);
			return PublicInfoUtil.toXml(orgres);
		}
		Date lastsyntime = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			lastsyntime = sf.parse(orgreq.getLastsyntime());
		} catch (ParseException e) {
			log.error("字符串" + orgreq.getLastsyntime() + "转换日期类型失败", e);
		}
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("GED_t!updatetime", lastsyntime));
		String sql = "select t from Organization t where t.isactive=1 ";
		List<Organization> orglist = dao.find(sql, filters);
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMMddHHmmss");
		if (orglist != null && orglist.size() > 0) {
			List<Org> orginfos = new ArrayList<Org>();
			for (Organization org : orglist) {
				Org orginfo = new Org();
				orginfo.setAddress(org.getAddress());
				orginfo.setCity(org.getCity());
				orginfo.setLinkman(org.getLinkman());
				orginfo.setOrgcode(org.getCode());
				orginfo.setOrgname(org.getName());
				orginfo.setOrgtype(org.getType());
				orginfo.setPhoneno(org.getPhoneno());
				orginfo.setStationlvl(org.getStationlvl());
				orginfo.setTrafficroute(org.getTrafficroute());
				orginfo.setUpdatetime(sf2.format(org.getUpdatetime()));
				orginfos.add(orginfo);
			}
			orgres.setOrgs(orginfos);
			orgres.setRecordcount(orglist.size());
			orgres.setRecordstart(1);
			orgres.setRecordend(orglist.size());
		} else {
			orgres.setRecordcount(0);
			orgres.setRecordstart(0);
			orgres.setRecordend(0);
		}
		orgres.getPublicresponse().setResultcode("0000");
		orgres.getPublicresponse().setResultmsg(
				exceptionService.getExceptionMsg("0000"));
		orgres.getPublicresponse().setReturntime(
				(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
						.format(new Date()));
		return PublicInfoUtil.toXml(orgres);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String stationsyn(String xmlString) {
//		log.info("stationsyn="+xmlString);
		StationSyncRequest stationreq;
		StationSyncResponse stationres = new StationSyncResponse();
		stationres
				.setPublicresponse(PublicInfoUtil.newPublicResponse());
		try {
			stationreq = (StationSyncRequest) XmlUtil.toBean(xmlString,
					StationSyncRequest.class);

		} catch (Exception e) {
			stationres.getPublicresponse().setResultcode("1001");
			stationres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("1001") + e.getMessage());
			log.error(stationres.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
			return PublicInfoUtil.toXml(stationres);
		}
		List<Station> stations = stationreq.getStations();
		boolean issuccess = true;
		for (Station sy : stations) {
			Organization organization = (Organization) dao
					.uniqueResult("select d from Organization d where d.code='"
							+ sy.getOrgcode() + "'");
			if (organization != null) {
				if (sy.getName() == null) {
					issuccess = false;
					stationres.getPublicresponse().setResultcode("1024");
					stationres.getPublicresponse().setResultmsg(
							exceptionService.getExceptionMsg("1024"));
					log.warn(stationres.getPublicresponse().getResultmsg()+xmlString);
					continue;
				} else if (sy.getStationcode() == null) {
					issuccess = false;
					stationres.getPublicresponse().setResultcode("1021");
					stationres.getPublicresponse().setResultmsg(
							exceptionService.getExceptionMsg("1021"));
					log.warn(stationres.getPublicresponse().getResultmsg()+xmlString);
					continue;
				}
				StationInfo station = (StationInfo) dao
						.uniqueResult(" select tb from StationInfo tb where tb.name='"
								+ sy.getName()
								+ "' and tb.code='"+sy.getStationcode()+"' and tb.orgid="
								+ organization.getId());
				try {
					if (station == null) {
						station = new StationInfo();
						station.setCreateby(0);
						station.setCreatetime(new Date());
					}
					userTransaction.begin();
					station.setCity(sy.getCity());
					station.setCode(sy.getStationcode());
					station.setCounty(sy.getCounty());
					station.setIsactive(true);
					station.setName(sy.getName());
					station.setOrgid(organization.getId());
					station.setProvince(sy.getProvince());
					station.setSecondname(sy.getSecondname());
					station.setStationlvl(sy.getStationlvl());
					station.setUpdateby(0);
					station.setUpdatetime(new Date());
					dao.merge(station);
					userTransaction.commit();
				} catch (Exception e) {
					log.error("站点同步出错"+xmlString, e);
					stationres.getPublicresponse().setResultcode("1019");
					stationres.getPublicresponse().setResultmsg(
							exceptionService.getExceptionMsg("1019")
									+ e.getMessage());
					try {
						if (userTransaction.getStatus() == 0 || userTransaction.getStatus() ==5) {
							userTransaction.rollback();
						} else {
							userTransaction.rollback();
						}
					} catch (Exception e2) {
						log.error("站点事务提交失败" + "请求字符串为：" + xmlString, e);
					}
					issuccess = false;
					continue;
				}
			} else {
				issuccess = false;
				stationres.getPublicresponse().setResultcode("1020");
				stationres.getPublicresponse().setResultmsg("中心无"+sy.getName()+"的所属机构,机构编码为"+sy.getOrgcode());
				log.error(stationres.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString);
				continue;
			}
		}
		if (issuccess == true) {
			stationres.getPublicresponse().setResultcode("0000");
			stationres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("0000"));
		}
		return PublicInfoUtil.toXml(stationres);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public String pricesyn(String xmlString) {
		PriceSynRequest pricereq;
		PriceSynResponse priceres = new PriceSynResponse();
		priceres.setPublicresponse(PublicInfoUtil.newPublicResponse());
		try {
			pricereq = (PriceSynRequest) XmlUtil.toBean(xmlString,
					PriceSynRequest.class);

		} catch (Exception e) {
			priceres.getPublicresponse().setResultcode("1001");
			priceres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("1001") + e.getMessage());
			log.error(priceres.getPublicresponse().getResultmsg() + "请求字符串为:" + xmlString, e);
			return PublicInfoUtil.toXml(priceres);
		}
		List<Price> prices = pricereq.getPrices();
		boolean issuccess = true;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMMddHHmmss");		
		for (Price sy : prices) {
			Organization organization = (Organization) dao
					.uniqueResult("select d from Organization d where d.code='"
							+ sy.getOrgcode() + "'");
			if (organization != null) {
				if (!checkPriceSynInfo(sy)) {
					issuccess = false;
					priceres.getPublicresponse().setResultcode("1023");
					priceres.getPublicresponse().setResultmsg(
							exceptionService.getExceptionMsg("1023"));
					continue;
				}
				Date departdate = new Date();
				try {
					departdate = sf.parse(sy.getDepartdate());
				} catch (ParseException e) {
					log.error("字符串" + sy.getDepartdate() + "转换日期类型失败", e);
					issuccess = false;
					priceres.getPublicresponse().setResultcode("1022");
					priceres.getPublicresponse().setResultmsg(
							sy.getDepartdate()
									+ exceptionService.getExceptionMsg("1022"));
					continue;
				}
				if (!checktime(sy.getDeparttime())) {
					issuccess = false;
					priceres.getPublicresponse().setResultcode("1021");
					priceres.getPublicresponse().setResultmsg(
							exceptionService.getExceptionMsg("1021"));
					continue;
				}
				Date updatetime = new Date();
				try {
					updatetime = sf2.parse(sy.getUpdatetime());
				} catch (ParseException e) {
					log.error("字符串" + sy.getUpdatetime() + "转换日期类型失败", e);
					issuccess = false;
					priceres.getPublicresponse().setResultcode("1051");
					priceres.getPublicresponse().setResultmsg(
							sy.getDepartdate()
									+ exceptionService.getExceptionMsg("1051"));
					continue;
				}
				try {
					userTransaction.begin();
					
					Map map = new HashMap();
					map.put("departdate", departdate);
					map.put("schedulecode", sy.getSchedulecode());
					map.put("reachstationcode", sy.getReachstationcode());
					map.put("seattype", sy.getSeattype());
					map.put("orgid", organization.getId());
					SchedulepriceBuffer sp = (SchedulepriceBuffer) dao.uniqueResult(SchedulepriceBuffer.class, map);
					
					if(sp==null){
						sp = new SchedulepriceBuffer();
						sp.setDepartdate(departdate);
						sp.setSchedulecode(sy.getSchedulecode());
						sp.setReachstationcode(sy.getReachstationcode());
						sp.setSeattype(sy.getSeattype());
						sp.setOrgid(organization.getId());
					}
					
					sp.setAreatype(sy.getAreatype());
					sp.setBusBrand(sy.getBusbrand());
					sp.setCansell(sy.getCansell().equals("1") ? true : false);
					sp.setCreateby(0);
					sp.setCreatetime(updatetime);
					sp.setDeparttime(sy.getDeparttime());
					sp.setDiscountprice(sy.getDiscountprice());
					sp.setEndstationcode(sy.getEndstationcode());
					sp.setEndtime(sy.getEndtime());
					sp.setFullprice(sy.getFullprice());
					sp.setHalfprice(sy.getHalfprice());
					sp.setIslinework(sy.isIslineschedule());
					sp.setIsovertime(sy.isIsovertime());
					sp.setRemainkidseats(sy.getRemainkidseats());
					sp.setRemainseats(sy.getRemainseats());
					sp.setRoutecode(sy.getRoutecode());
					sp.setRoutename(sy.getRoutename());
					sp.setRundistance(sy.getRundistance());
					sp.setRuntime(sy.getRuntime());
					sp.setScheduletype(sy.getScheduletype());
					sp.setSchstatus(sy.getSchstatus());
					sp.setStarttime(sy.getStarttime());
					sp.setStudentprice(sy.getStudentprice());
					sp.setTicketentrance(sy.getTicketentrance());
					sp.setTopprice(sy.getTopprice());
					sp.setTotalseats(sy.getTotalseats());
					sp.setUpdateby(0);
					sp.setUpdatetime(new Date());
					sp.setVehiclelvl(sy.getVehiclelvl());
					sp.setVehicleno(sy.getVehicleno());
					sp.setVehicletype(sy.getVehicletype());
					sp.setWaitingroom(sy.getWaitingroom());
					dao.merge(sp);
					userTransaction.commit();
				} catch (Exception e) {
					priceres.getPublicresponse().setResultcode("1023");
					priceres.getPublicresponse().setResultmsg(
							exceptionService.getExceptionMsg("1023")
									+ e.getMessage());
					log.error(priceres.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
					try {
						if (userTransaction.getStatus() == 0 || userTransaction.getStatus() ==5) {
							userTransaction.rollback();
						} else {
							userTransaction.rollback();
						}
					} catch (Exception e2) {
						log.error("班次事务提交失败" + "请求字符串为：" + xmlString, e);
					}
					issuccess = false;
					continue;
				}
			}
			else{
				log.error("班次计划同步失败,所属机构中心不存在编码为:"+sy.getOrgcode()+"的机构");
			}
		}
		if (issuccess) {
			// 数据保存成功
			priceres.getPublicresponse().setResultcode("0000");
			priceres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("0000"));
		}
		return PublicInfoUtil.toXml(priceres);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String ticketsyn(String xmlString) {
		TicketSyncRequest ticketreq;
		Ticketsynresponse ticketres = new Ticketsynresponse();
		ticketres.setPublicresponse(PublicInfoUtil.newPublicResponse());
		try {
			ticketreq = (TicketSyncRequest) XmlUtil.toBean(xmlString,
					TicketSyncRequest.class);

		} catch (Exception e) {
			ticketres.getPublicresponse().setResultcode("1001");
			ticketres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("1001") + e.getMessage());
			log.error(ticketres.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
			return PublicInfoUtil.toXml(ticketres);
		}
		List<Ticket> tickets = ticketreq.getTickets();
		boolean issuccess = true;
		for (Ticket sy : tickets) {
			List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
			filters.clear();
			filters.add(new PropertyFilter("EQS_t!netticketid", sy
					.getNetticketid()));
			String sql = "select t from Ticketsell t where 1=1 ";
			Ticketsell ts = (Ticketsell) dao.uniqueResult(sql, filters);
            if(ts==null){
            	issuccess=false;
            	//未找到该车票信息
				ticketres.getPublicresponse().setResultcode("1031");
				ticketres.getPublicresponse().setResultmsg(
						exceptionService.getExceptionMsg("1031")+":"+sy
						.getNetticketid());
            	continue;
            }
			try {
				userTransaction.begin();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				if(sy.getTicketno() !=null && !("").equals(sy.getTicketno())){
					
					ts.setTicketno(sy.getTicketno());
				}
				ts.setTicketstatus(sy.getTicketstatus());
				if(sy.getReturnvoucherno() !=null && !("").equals(sy.getReturnvoucherno())){
					Ticketreturn ticketreturn=(Ticketreturn) dao.uniqueResult("select t from Ticketreturn t where t.ticketid="+ts.getId());
					ticketreturn.setReturnvoucherno(sy.getReturnvoucherno());
					dao.merge(ticketreturn);
				}
				if(sy.getTaketime() !=null && !("").equals(sy.getTaketime())){
					ts.setTaketime(sdf.parse(sy.getTaketime()));
					ts.setTakestatus(sy.getTakestatus());
				}
				if(sy.getInsuranceno() !=null && !("").equals(sy.getInsuranceno())){
					Insurance insurance=(Insurance) dao.uniqueResult("select i from Insurance i where i.sellid="+ts.getId());
					insurance.setInsuranceno(sy.getInsuranceno()); 
					dao.merge(insurance);
				}
				if (ts.getTicketstatus().equals("1")) {
					ts.setReturnfee(sy.getReturnfee());
					ts.setReturnrate(sy.getReturnrate());
					if(sy.getReturntime()!=null&&!sy.getReturntime().trim().equals("")){
						Date returntime = null;
						try {
							
							returntime = sdf.parse(sy.getReturntime());
						} catch (Exception e) {
							log.error("字符串" + sy.getReturntime() + "时间日期转换失败", e);
							// 退票时间格式不对
							ticketres.getPublicresponse().setResultcode("1029");
							ticketres.getPublicresponse().setResultmsg(
									exceptionService.getExceptionMsg("1029"));
							log.error(ticketres.getPublicresponse()
									.getResultmsg());
							return PublicInfoUtil.toXml(ticketres);
	
						}
						ts.setReturntime(returntime);
					}
				}
				ts.setUpdatetime(new Date());
				dao.merge(ts);
				userTransaction.commit();
			} catch (Exception e) {
				ticketres.getPublicresponse().setResultcode("1019");
				ticketres.getPublicresponse().setResultmsg(
						exceptionService.getExceptionMsg("1019")
								+ e.getMessage());
				log.error(ticketres.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
				try {
					if (userTransaction.getStatus() == 0 || userTransaction.getStatus() ==5) {
						userTransaction.rollback();
					} else {
						userTransaction.rollback();
					}
				} catch (Exception e2) {
					log.error("电子票事务提交失败" + "请求字符串为：" + xmlString, e);
				}
				e.printStackTrace();
				issuccess = false;
				continue;
			}
		}

		if (issuccess == true) {
			ticketres.getPublicresponse().setResultcode("0000");
			ticketres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("0000"));
		}
		return PublicInfoUtil.toXml(ticketres);
	}

	private boolean checkPriceSynInfo(Price sy) {
		if (sy.getDepartdate() == null || sy.getSchedulecode() == null
				|| sy.getDeparttime() == null || sy.getRoutename() == null
				|| sy.getRoutecode() == null
				|| sy.getReachstationcode() == null || sy.getAreatype() == null
				|| sy.getEndstationcode() == null || sy.getSchstatus() == null
				|| sy.getSeattype() == null || sy.getTicketentrance() == null) {
			return false;
		} else {
			return true;
		}
	}

	private boolean checktime(String departtime) {
		String[] times = departtime.split(":");
		if (departtime.length() != 5 || times.length != 2) {
			return false;
		}
		int hh = Integer.valueOf(times[0]);
		int mm = Integer.valueOf(times[1]);
		if (hh < 0 || hh > 24 || mm < 0 || mm > 60) {
			return false;
		}

		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String pricedelsyn(String xmlString) {
		Pricedelsynrequest pricereq;
		Pricesdelynresponse priceres = new Pricesdelynresponse();
		priceres.setPublicresponse(PublicInfoUtil.newPublicResponse());
		try {
			pricereq = (Pricedelsynrequest) XmlUtil.toBean(xmlString,
					Pricedelsynrequest.class);

		} catch (Exception e) {
			priceres.getPublicresponse().setResultcode("1001");
			priceres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("1001") + e.getMessage());
			log.error(priceres.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
			return PublicInfoUtil.toXml(priceres);
		}
		
		List<Pricedel> prices = pricereq.getPrices();
		boolean issuccess = true;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMMddHHmmss");
		int i=0;
		for (Pricedel sy : prices) {
			Organization organization = (Organization) dao
					.uniqueResult("select d from Organization d where d.code='"
							+ sy.getOrgcode() + "'");
			if (organization != null) {
				Date departdate = new Date();
				try {
					departdate = sf.parse(sy.getDepartdate());
				} catch (ParseException e) {
					log.error("字符串" + sy.getDepartdate() + "时间日期转换失败", e);
					issuccess = false;
					priceres.getPublicresponse().setResultcode("1022");
					priceres.getPublicresponse().setResultmsg(
							sy.getDepartdate()
									+ exceptionService.getExceptionMsg("1022"));
					continue;
				}
				Date updatetime = new Date();
				try {
					updatetime = sf2.parse(sy.getCreatetime());
				} catch (ParseException e) {
					log.error("字符串" + sy.getCreatetime() + "时间日期转换失败", e);
					issuccess = false;
					priceres.getPublicresponse().setResultcode("1051");
					priceres.getPublicresponse().setResultmsg(
							sy.getDepartdate()
									+ exceptionService.getExceptionMsg("1051"));
					continue;
				}
				try {
					userTransaction.begin();
					SchedulepricedelBuffer sp = new SchedulepricedelBuffer();
					sp.setCreateby(0);
					sp.setCreatetime(new Date());
					sp.setDepartdate(departdate);
					sp.setOrgid(organization.getId());
					sp.setReachstationcode(sy.getReachstationcode());
					sp.setSchedulecode(sy.getSchedulecode());
					sp.setSeattype(sy.getSeattype());
					sp.setUpdateby(0);
					sp.setUpdatetime(updatetime);
					dao.merge(sp);
					i++;
					userTransaction.commit();
				} catch (Exception e) {
					priceres.getPublicresponse().setResultcode("1023");
					priceres.getPublicresponse().setResultmsg(
							exceptionService.getExceptionMsg("1023")
									+ e.getMessage());
					log.error(priceres.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
					try {
						if (userTransaction.getStatus() == 0 || userTransaction.getStatus() ==5) {
							userTransaction.rollback();
						} else {
							userTransaction.rollback();
						}
					} catch (Exception e2) {
						log.error("班次删除事务提交失败" + "请求字符串为：" + xmlString, e);
					}
					issuccess = false;
					continue;
				}
			}
		}
		log.info("has "+i+" recordcount save suceess!");
		if (issuccess) {
			// 数据保存成功
			priceres.getPublicresponse().setResultcode("0000");
			priceres.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("0000"));
		}
		
		return PublicInfoUtil.toXml(priceres);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String vehiclesyn(String xmlString) {
		VehicleSynRequest request = null;
		VehicleSynResponse response = new VehicleSynResponse();
		response.setPublicresponse(PublicInfoUtil.newPublicResponse());
		try {
			request = (VehicleSynRequest) XmlUtil.toBean(xmlString,
					VehicleSynRequest.class);

		} catch (Exception e) {
			response.getPublicresponse().setResultcode("1001");
			response.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("1001") + e.getMessage());
			log.error(response.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
			return PublicInfoUtil.toXml(response);
		}
		List<Vehicleinfo> vehicleinfos = request.getVehicles();
		Vehicle vehicle = null;
		boolean flag = true;
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHH");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		for(Vehicleinfo vehicleinfo : vehicleinfos){
			Organization organization = (Organization) dao
					.uniqueResult("select d from Organization d where d.code='"
							+ vehicleinfo.getOrgcode() + "'");
			if(organization==null){
				flag = false;
				response.getPublicresponse().setResultcode("1020");
				response.getPublicresponse().setResultmsg(
						vehicleinfo.getUpdatetime()
								+ exceptionService.getExceptionMsg("1020"));
				log.error("中心无此机构信息！",vehicleinfo.getOrgcode());
				continue;
			}
			Date updatetime = new Date();
			try {
				updatetime = sdf.parse(vehicleinfo.getUpdatetime());
			} catch (ParseException e) {
				flag = false;
				response.getPublicresponse().setResultcode("1051");
				response.getPublicresponse().setResultmsg(
						vehicleinfo.getUpdatetime()
								+ exceptionService.getExceptionMsg("1051"));
				log.error("车牌同步更新时间格式不对" + vehicleinfo.getUpdatetime(), e);
				continue;
			}
			Date factorydate = null;
			if(vehicleinfo.getFactorydate()!=null&&"".equals(vehicleinfo.getFactorydate())){
				try{
					factorydate = sf.parse(vehicleinfo.getFactorydate());
				} catch (ParseException e) {
					flag = false;
					response.getPublicresponse().setResultcode("1052");
					response.getPublicresponse().setResultmsg(
							vehicleinfo.getUpdatetime()
									+ exceptionService.getExceptionMsg("1052"));
					log.error(vehicleinfo.getUpdatetime() + "出厂日期格式不对",e);
					continue;
				}
			}
			vehicle = (Vehicle) dao.uniqueResult(" select v from Vehicle v where v.vehicleno='"	+ vehicleinfo.getVehicleno() + "' and v.orgid=" + organization.getId());
			try{
				if(vehicle==null){
					vehicle = new Vehicle();
					vehicle.setCreatetime(new Date());
				}
				userTransaction.begin();
				vehicle.setOrgid(organization.getId());
				vehicle.setVehicleno(vehicleinfo.getVehicleno());
				vehicle.setVehiclelvl(vehicleinfo.getVehiclelvl());
				vehicle.setVehicletype(vehicleinfo.getVehicletype());
				vehicle.setRationseatnum(vehicleinfo.getRationseatnum());
				vehicle.setBusbrand(vehicleinfo.getBusbrand());
				vehicle.setSeatnum(vehicleinfo.getSeatnum());
				vehicle.setFactorydate(factorydate);
				vehicle.setUnitname(vehicleinfo.getUnitname());
				vehicle.setUpdatetime(updatetime);
				dao.merge(vehicle);
				userTransaction.commit();
			}catch(Exception e) {
				response.getPublicresponse().setResultcode("-1");
				response.getPublicresponse().setResultmsg(
						exceptionService.getExceptionMsg("-1")
								+ e.getMessage());
				log.error(response.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
				try {
					if (userTransaction.getStatus() == 0 || userTransaction.getStatus() ==5) {
						userTransaction.rollback();
					} else {
						userTransaction.rollback();
					}
				} catch (Exception e2) {
					log.error("车牌事务提交失败" + "请求字符串为：" + xmlString, e);
				}
				flag = false;
				continue;
			}
		}
		if(flag){
			response.getPublicresponse().setResultcode("0000");
			response.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("0000"));
			log.info("vehiclesyn同步成功！");
		}
		return PublicInfoUtil.toXml(response);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String departpassengersyn(String xmlString) {
		DepartpassengerSynRequest request = null;
		DepartpassengerSynResponse response = new DepartpassengerSynResponse();
		response.setPublicresponse(PublicInfoUtil.newPublicResponse());
		try {
			request = (DepartpassengerSynRequest) XmlUtil.toBean(xmlString,
					DepartpassengerSynRequest.class);

		} catch (Exception e) {
			response.getPublicresponse().setResultcode("1001");
			response.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("1001") + e.getMessage());
			log.error(response.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
			return PublicInfoUtil.toXml(response);
		}
		List<Departpassengerinfo> departpassengerinfos = request.getDepartpassengers();
		Departpassengerbuffer departpassengerbuffer = null;
		boolean flag = true;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		for(Departpassengerinfo departpassengerinfo : departpassengerinfos){
			Organization organization = (Organization) dao.uniqueResult("select d from Organization d where d.code='"+ departpassengerinfo.getOrgcode() + "'");
			if(organization==null){
				flag = false;
				response.getPublicresponse().setResultcode("1020");
				response.getPublicresponse().setResultmsg(
						departpassengerinfo.getUpdatetime()
								+ exceptionService.getExceptionMsg("1020"));
				log.error("中心无此机构信息！" + departpassengerinfo.getOrgcode());
				continue;
			}
			Date departdate = new Date();
			try {
				departdate = sf.parse(departpassengerinfo.getDepartdate());
			} catch (ParseException e) {
				flag = false;
				response.getPublicresponse().setResultcode("1022");
				response.getPublicresponse().setResultmsg(
						departpassengerinfo.getDepartdate()
								+ exceptionService.getExceptionMsg("1022"));
				log.error("发车日期格式不对" + departpassengerinfo.getDepartdate(), e);
				continue;
			}
			Date updatetime = new Date();
			try {
				updatetime = sdf.parse(departpassengerinfo.getUpdatetime());
			} catch (ParseException e) {
				flag = false;
				response.getPublicresponse().setResultcode("1051");
				response.getPublicresponse().setResultmsg(
						departpassengerinfo.getUpdatetime()
								+ exceptionService.getExceptionMsg("1051"));
				log.error(departpassengerinfo.getUpdatetime() + "更新时间格式不对", e);
				continue;
			}
			try{
				StationInfo stationinfo = (StationInfo) dao.uniqueResult("select t from StationInfo t where t.code='" + departpassengerinfo.getReachstationcode() + "' and t.orgid=" + organization.getId());
				if(stationinfo==null){
					flag = false;
					response.getPublicresponse().setResultcode("-1");
					response.getPublicresponse().setResultmsg(
							departpassengerinfo.getUpdatetime()
							+ exceptionService.getExceptionMsg("-1"));
					log.error("无此到站编码" + departpassengerinfo.getReachstationcode());
					continue;
				}
				
				departpassengerbuffer = (Departpassengerbuffer) dao.uniqueResult(" select d from Departpassenger d where d.departdate='"
				+ departpassengerinfo.getDepartdate() + "' and d.schedulecode='" + departpassengerinfo.getSchedulecode()
				+ "' and d.reachstationcode="+departpassengerinfo.getReachstationcode()+" and d.orgid=" + organization.getId());
				if(departpassengerbuffer==null){
					departpassengerbuffer = new Departpassengerbuffer();
				}
				
				String isoriginator = departpassengerinfo.getIsoriginator();
				if(isoriginator!=null&&!"".equals(isoriginator)){
					departpassengerbuffer.setIsoriginator(Integer.parseInt(isoriginator));
				}
				
				userTransaction.begin();
				departpassengerbuffer.setCreatetime(new Date());
				departpassengerbuffer.setOrgid(organization.getId());
				departpassengerbuffer.setDepartdate(departdate);
				departpassengerbuffer.setSchedulecode(departpassengerinfo.getSchedulecode());
				departpassengerbuffer.setReachstationcode(departpassengerinfo.getReachstationcode());
				departpassengerbuffer.setVehicleno(departpassengerinfo.getVehicleno());
				departpassengerbuffer.setMoneysellbyotherstation(new BigDecimal(departpassengerinfo.getMoneysellbyotherstation()));
				departpassengerbuffer.setMoneysellbyotherway(new BigDecimal(departpassengerinfo.getMoneybysellotherway()));
				departpassengerbuffer.setMoneysellbystation(new BigDecimal(departpassengerinfo.getMoneysellbystation()));
				departpassengerbuffer.setMoneysellbywebsite(new BigDecimal(departpassengerinfo.getMoneybuysellwebsite()));
				departpassengerbuffer.setNumsellbyotherstation(departpassengerinfo.getNumsellbyotherstation());
				departpassengerbuffer.setNumsellbyotherway(departpassengerinfo.getNumbysellotherway());
				departpassengerbuffer.setNumsellbystation(departpassengerinfo.getNumsellbystation());
				departpassengerbuffer.setNumsellbywebsite(departpassengerinfo.getNumbuysellwebsite());
				departpassengerbuffer.setUpdatetime(updatetime);
				dao.merge(departpassengerbuffer);
				userTransaction.commit();
			}catch(Exception e) {
				response.getPublicresponse().setResultcode("-1");
				response.getPublicresponse().setResultmsg(
						exceptionService.getExceptionMsg("-1")
								+ e.getMessage());
				log.error(response.getPublicresponse().getResultmsg() + "请求字符串为：" + xmlString, e);
				try {
					if (userTransaction.getStatus() == 0 || userTransaction.getStatus() ==5) {
						userTransaction.rollback();
					} else {
						userTransaction.rollback();
					}
				} catch (Exception e2) {
					log.error("发班运量事务提交失败" + "请求字符串为：" + xmlString, e);
				}
				flag = false;
				continue;
			}
		}
		if(flag){
			response.getPublicresponse().setResultcode("0000");
			response.getPublicresponse().setResultmsg(
					exceptionService.getExceptionMsg("0000"));
			log.info("departpassengersyn同步成功！");
		}
		return PublicInfoUtil.toXml(response);
	}*/
}
